package com.xinfang.louxun.base.adapter

import android.os.Parcelable
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.PagerAdapter
import java.util.*

class BaseFragmentAdapter: FragmentStatePagerAdapter {
    private var fragmentList: List<Fragment>
    private var titleList: List<String>? = null

    constructor(
        fm: FragmentManager?,
        fragmentList: List<Fragment>,
        titles: Array<String>
    ) : super(fm) {
        this.fragmentList = fragmentList
        titleList =ArrayList(Arrays.asList(*titles))
    }

    constructor(
        fm: FragmentManager?,
        fragmentList: List<Fragment>,
        titleList: List<String>
    ) : super(fm) {
        this.fragmentList = fragmentList
        this.titleList = titleList
    }

    override   fun getItem(position: Int): Fragment {
        return fragmentList[position]
    }

    override fun saveState(): Parcelable? {
        return null
    }

    override fun getCount(): Int = fragmentList.size

    override fun getPageTitle(position: Int): CharSequence {
        return (titleList?.get(position) as CharSequence)
    }

    override  fun getItemPosition(`object`: Any): Int {
        return PagerAdapter.POSITION_NONE
    }

    fun recreateItems(fragmentList: List<Fragment>,titleList: List<String>) {
        this.fragmentList = fragmentList
        this.titleList = titleList
        notifyDataSetChanged()
    }

    fun recreateItems(fragmentList: List<Fragment>, titleList: Array<String>) {
        this.fragmentList = fragmentList
        this.titleList =
            ArrayList(Arrays.asList(*titleList))
        notifyDataSetChanged()
    }
}